package com.jxj.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxj.entity.Resp;
import com.jxj.entity.ResumeVO;
import com.jxj.entity.UserResumeVO;
import com.jxj.entity.gen.Adminuser;
import com.jxj.service.UserService;
import com.jxj.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Cambria
 * @creat 2021/8/15 11:37
 */
@Slf4j
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping({"/user/record"})
    Resp<UserResumeVO> selectAllResumeByUid(@RequestHeader("Authorization") String token) {
        token = token.split(" ")[1];
        Integer uid = JWTUtil.getUserId(token);
        log.info("uid {} get resumes" , uid);
        return new Resp(HttpStatus.OK , userService.selectAllResumeByUid(uid));
    }

    @PostMapping({"/user/bind"})
    Resp<String> register(String code) {
        String token = userService.register(code);

        if (token == null) {
            return new Resp<>(HttpStatus.BAD_REQUEST , token);
        } else {
            return new Resp<>(HttpStatus.OK , token);
        }
    }

    @PostMapping({"/joinSt"})
    ResponseEntity<FileSystemResource> joinSt(ResumeVO resume) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .header(HttpHeaders.CONTENT_DISPOSITION , "attachment;filename=resume.doc")
                .header(HttpHeaders.CONTENT_TYPE , "application/msword")
                .body(new FileSystemResource(userService.resumeParseDoc(resume)));
    }

    @PostMapping({"/loginorg"})
    Resp<JSONObject> adminRegister(Adminuser admin) {
        JSONObject jsonObject = userService.adminRegister(admin);
        if (jsonObject.getString("token") == null) {
            return new Resp<>(HttpStatus.FORBIDDEN , jsonObject);
        }

        return new Resp<>(HttpStatus.OK , jsonObject);
    }
}
